﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Resources;
using System.IO;
using System.Net;

namespace DSLFactory.VSCTDesigner
{
    partial class Bitmap
    {
        private System.Drawing.Bitmap _bitmap;

        public string GetDisplayNameValue()
        {
            try
            {
                return !String.IsNullOrEmpty(hRef) ? Path.GetFileName(hRef) : !String.IsNullOrEmpty(resId) ? resId : Guid;
            }
            catch
            {
                return Guid;
            }
        }

        protected override void MergeConfigure(Microsoft.VisualStudio.Modeling.ElementGroup elementGroup)
        {
            base.MergeConfigure(elementGroup);
            this.Guid = System.Guid.NewGuid().ToString("B");
        }

        internal System.Drawing.Bitmap ResolveImage(string iconId)
        {
            if( _bitmap == null )
            {

                if( !String.IsNullOrEmpty(hRef) )
                {
                    string currentDirectory = Path.GetDirectoryName(ShellHelper.GetModelFileName(this.Store));
                    try
                    {
                        string filePath = Path.Combine(currentDirectory, hRef);
                        if( File.Exists(filePath) )
                            _bitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(filePath);
                    }
                    catch { }

                    if( _bitmap == null )
                    {
                        try
                        {
                            Uri fileURI = new Uri(hRef);
                            if( fileURI.IsAbsoluteUri )
                            {
                                using( WebResponse response = WebRequest.Create(fileURI).GetResponse() )
                                {
                                    using( Stream responseStream = response.GetResponseStream() )
                                    {
                                        _bitmap = (System.Drawing.Bitmap)Image.FromStream(responseStream);
                                    }
                                }
                            }
                            else
                            {
                                Uri resolvedURI = new Uri(new Uri(currentDirectory), fileURI);
                                using( WebResponse response = WebRequest.Create(resolvedURI).GetResponse() )
                                {
                                    using( Stream responseStream = response.GetResponseStream() )
                                    {
                                        _bitmap = (System.Drawing.Bitmap)Image.FromStream(responseStream);
                                    }
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }

            if( _bitmap != null )
            {
                int row = CalculateRowIndex(iconId);
                try
                {
                    return iconId == null ? (System.Drawing.Bitmap)_bitmap.Clone() : row < 0 ? null : _bitmap.Clone(new Rectangle(row * 16, 0, 16, 16), System.Drawing.Imaging.PixelFormat.DontCare);
                }
                catch { }
            }
            return null;
        }

        private int CalculateRowIndex(string iconId)
        {
            if( String.IsNullOrEmpty(iconId) )
                return -1;

            int idx = 0;
            foreach( string val in usedList.Split(new char[] { ',' }) )
            {
                if( val.Trim().Equals(iconId, StringComparison.CurrentCultureIgnoreCase) )
                    return idx;
                idx++;
            }
            return -1;
        }
    }
}
